using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Wrath.Windows.Forms;
using Wrath.Xml;
using Wrath.Xml.Objects;
using System.IO;
using Wrath.RuleForge.Property;

namespace Wrath.RuleForge.Imaging
{
    public partial class FrameEditor : DockContent, Interfaces.ICanUndo, Interfaces.ICanSave
    {
        private Element frameElement;

        public FrameEditor(Element frameElement)
        {
            InitializeComponent();

            this.frameElement = frameElement;
            comboBoxSelectedArea.SelectedIndex = 0;

            frameEntryBox.SetFrame += new EventHandler(frameEntryBox_SetFrame);
            frameEntryBox.CancelFrame += new EventHandler(frameEntryBox_CancelFrame);
        }

        void frameEntryBox_CancelFrame(object sender, EventArgs e)
        {
            frameEntryBox.Frame = frameGuideControl.SelectedRectangle;
        }

        void frameEntryBox_SetFrame(object sender, EventArgs e)
        {
            frameGuideControl.CreateNewFrame(frameEntryBox.Frame);
        }

        private void FrameEditor_Load(object sender, EventArgs e)
        {
            if (frameElement != null)
            {
                frameGuideControl.ParseElement(frameElement);
                this.TabText = frameGuideControl.FrameName + "  ";

                frameEntryBox.Frame = frameGuideControl.SelectedRectangle;
            }
        }

        #region ICanUndo Members

        public bool UndoEnabled
        {
            get
            {
                return frameGuideControl.UndoEnabled;
            }
        }

        public void Undo()
        {
            frameGuideControl.Undo();

            if (!frameGuideControl.UndoEnabled)
            {
                toolStripButtonSave.Enabled = false;
                this.TabText = frameGuideControl.FrameName + "  ";
            }
        }

        #endregion

        #region ICanSave Members

        public void Save()
        {
            // Call FrameGuideControl Save.
            if (frameGuideControl.Modified)
            {
                frameGuideControl.Save();
                this.TabText = frameGuideControl.FrameName + "  ";
                toolStripButtonSave.Enabled = false;
            }
        }

        #endregion

        private void comboBoxSelectedArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxSelectedArea.SelectedItem is string)
            {
                string selectedArea = (string)comboBoxSelectedArea.SelectedItem;

                if (selectedArea == "None") frameGuideControl.SelectArea(RectangleAreas.None);
                else if (selectedArea == "Decal") frameGuideControl.SelectArea(RectangleAreas.Decal);
                else if (selectedArea == "Top Left") frameGuideControl.SelectArea(RectangleAreas.TopLeft);
                else if (selectedArea == "Top Middle") frameGuideControl.SelectArea(RectangleAreas.Top);
                else if (selectedArea == "Top Right") frameGuideControl.SelectArea(RectangleAreas.TopRight);
                else if (selectedArea == "Left") frameGuideControl.SelectArea(RectangleAreas.Left);
                else if (selectedArea == "Middle") frameGuideControl.SelectArea(RectangleAreas.Middle);
                else if (selectedArea == "Right") frameGuideControl.SelectArea(RectangleAreas.Right);
                else if (selectedArea == "Bottom Left") frameGuideControl.SelectArea(RectangleAreas.BottomLeft);
                else if (selectedArea == "Bottom Middle") frameGuideControl.SelectArea(RectangleAreas.Bottom);
                else if (selectedArea == "Bottom Right") frameGuideControl.SelectArea(RectangleAreas.BottomRight);

                frameEntryBox.Frame = frameGuideControl.SelectedRectangle;
            }
        }

        private void frameGuideControl_MouseMove(object sender, MouseEventArgs e)
        {
            this.statusXY.Text = e.X.ToString() + "," + e.Y.ToString();
        }

        private void frameGuideControl_SelectedAreaChanged(object sender, EventArgs e)
        {
            if (frameGuideControl.SelectedArea == RectangleAreas.None) comboBoxSelectedArea.SelectedItem = "None";
            else if (frameGuideControl.SelectedArea == RectangleAreas.TopLeft) comboBoxSelectedArea.SelectedItem = "Top Left";
            else if (frameGuideControl.SelectedArea == RectangleAreas.Top) comboBoxSelectedArea.SelectedItem = "Top Middle";
            else if (frameGuideControl.SelectedArea == RectangleAreas.TopRight) comboBoxSelectedArea.SelectedItem = "Top Right";
            else if (frameGuideControl.SelectedArea == RectangleAreas.Left) comboBoxSelectedArea.SelectedItem = "Left";
            else if (frameGuideControl.SelectedArea == RectangleAreas.Middle) comboBoxSelectedArea.SelectedItem = "Middle";
            else if (frameGuideControl.SelectedArea == RectangleAreas.Right) comboBoxSelectedArea.SelectedItem = "Right";
            else if (frameGuideControl.SelectedArea == RectangleAreas.BottomLeft) comboBoxSelectedArea.SelectedItem = "Bottom Left";
            else if (frameGuideControl.SelectedArea == RectangleAreas.Bottom) comboBoxSelectedArea.SelectedItem = "Bottom Middle";
            else if (frameGuideControl.SelectedArea == RectangleAreas.BottomRight) comboBoxSelectedArea.SelectedItem = "Bottom Right";
            else if (frameGuideControl.SelectedArea == RectangleAreas.Decal) comboBoxSelectedArea.SelectedItem = "Decal";

            frameEntryBox.Frame = frameGuideControl.SelectedRectangle;
        }

        private void toolStripButtonDragNewFrame_Click(object sender, EventArgs e)
        {
            frameGuideControl.DragNew = !frameGuideControl.DragNew;
        }

        private void frameGuideControl_NewFrameComplete(object sender, EventArgs e)
        {
            toolStripButtonDragNewFrame.Checked = false;
            this.TabText = frameGuideControl.FrameName + " *";
            toolStripButtonSave.Enabled = true;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            frameGuideControl.LockAreas = !frameGuideControl.LockAreas;
        }

        private void toolStripButtonManualEntry_Click(object sender, EventArgs e)
        {
            if (toolStripButtonManualEntry.Checked)
            {
                frameEntryBox.Hide();
                toolStripButtonManualEntry.Checked = false;
            }
            else
            {
                frameEntryBox.Show();
                toolStripButtonManualEntry.Checked = true;
            }
        }

        private void frameGuideControl_SelectedAreaModified(object sender, EventArgs e)
        {
            frameEntryBox.Frame = frameGuideControl.SelectedRectangle;
            this.TabText = frameGuideControl.FrameName + " *";
            toolStripButtonSave.Enabled = true;
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to remove this frame?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                frameGuideControl.DeleteSelectedArea();
            }
        }

        private void frameGuideControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control)
            {
                if (e.KeyCode == Keys.Z)
                {
                    Undo();
                }
                else if (e.KeyCode == Keys.S)
                {
                    Save();
                }
            }
        }

        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void FrameEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (frameGuideControl.Modified)
            {
                DialogResult result = MessageBox.Show("This Frame Definition has been modified. Do you want to save the changes?", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
                if (result != DialogResult.No)
                {
                    if (result == DialogResult.Yes)
                    {
                        Save();
                    }
                    else if (result == DialogResult.Cancel)
                    {
                        e.Cancel = true;
                    }
                }
            }
        }
    }
}